const http=require("http");
const fs=require("fs");
const template=require("art-template");

let user={
    id:123456,
    name:"张三",
    phone:"13879568475"
};

let users=[
    {id:1,name:"alice",phone:"13567876543",flag:true},
    {id:2,name:"tom",phone:"13567876343",flag:false},
    {id:3,name:"cici",phone:"13567876542",flag:true},
    {id:4,name:"lili",phone:"13567876541",flag:true},
    {id:5,name:"Eric",phone:"13567872541",flag:false}
   
];
let server=http.createServer(function(request,response){
    let path=request.url;
    switch(path){
        case "/":
            let temp=fs.readFileSync("./template/users.html").toString();
            temp=temp.replace("{{id}}",user.id);
            temp=temp.replace("{{name}}",user.name);
            temp=temp.replace("{{phone}}",user.phone);
            response.end(temp);
            break;

        case "/list":
            let temp2=fs.readFileSync("./template/list.html").toString();
            let str="";
            for(let u of users){
                str+=`
                    <tr>
                        <td>${u.id}</td>
                        <td>${u.name}</td>
                        <td>${u.phone}</td>
                        <td >${u.flag?"是":"否"}</td>
                    </tr>
                `;
            }
            temp2=temp2.replace("{{list}}",str);
            response.end(temp2);
            break;
             // npm install art-template 安装三方模块
        case "/user":
            let temp3=fs.readFileSync("./template/users.html").toString();//string
            let strs=template.render(temp3,user);
            response.end(strs);
            break;
        case "/newList":
            let temp4=fs.readFileSync("./template/artList.html").toString();//string
            let s=template.render(temp4,{"users":users});//返回的是字符串
            response.end(s);
            break;

           
            
    }

});

server.listen(3000);